<?php

namespace App\Controller\Wap;

use App\Entity\ShopGoods;
use App\Entity\UserAddress;
use App\Entity\UserLikesProduct;
use App\Model\AreaModel;
use App\Model\CityModel;
use App\Model\ProvinceModel;
use App\Model\ShopGoodsModel;
use App\Model\UserAddressModel;
use App\Model\UserLikesProductModel;
use App\Base\BaseController4User;
use App\Model\LazyCommon\CCM;
use App\Model\LazyCommon\PlatConfigModel;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;


/**
 * @route("/wap")
 */
class AddressController extends BaseController4User
{

    private function initData()
    {
        return $this->data;
    }

    /**
     * @route("/address/index",name="UserAppAddressIndex")
     */
    public function UserAppAddressIndex()
    {
        $this->initData();
        $uid = $this->needToBeUser();
        $UserAddressModel = new UserAddressModel($this->getDoctrine());
        $address = $UserAddressModel->getByUid($uid);
        foreach ($address as &$row) {
            $row = $this->toArray($row);
        }
        if (!$address) {
            $address = "";
        }

        $this->data["list"] = $address;
//        var_dump($address);
        return $this->render("App/Wap/address_index.php.twig",$this->data);
    }
    /**
     * @route("/address/indexJson",name="UserAppAddressIndexJson")
     */
    public function UserAppAddressIndexJson()
    {
        $this->initData();
        $uid = $this->needToBeUser();
        $UserAddressModel = new UserAddressModel($this->getDoctrine());
        $address = $UserAddressModel->getByUid($uid);
        foreach ($address as &$row) {
            $row = $this->toArray($row);
        }
        $this->data["list"] = $address;
        return $this->json_out(1,"获取数据成功",$this->data);
    }



    /**
     * @route("/address/get",name="UserAppAddressGet")
     */
    public function UserAppAddressGet()
    {
        $uid = $this->needToBeUser();
        $UserAddressModel = new UserAddressModel($this->getDoctrine());
        $address = $UserAddressModel->getDefault($uid);
        $this->toArray($address);
        return $this->json_out(1,"获取成功",$address);
    }



    /**
     * @route("/address/add",name="UserAppAddressAdd")
     */
    public function UserAppAddressAdd()
    {
        $data = $this->initData();
        $uid = $this->needToBeUser();
        if($id = $this->getParamRequired("id")){
            //编辑[]
            $UserAddressModel = new UserAddressModel($this->getDoctrine());
            $AreaModel = new AreaModel($this->getDoctrine());
            $CityModel = new CityModel($this->getDoctrine());
            $ProvinceModel = new ProvinceModel($this->getDoctrine());
            $UserAddress = $UserAddressModel->getById($id);
            if ($UserAddress){
                $p = $UserAddress->getProvince();
                $c = $UserAddress->getCity();
                $a = $UserAddress->getArea();
                $pName = $ProvinceModel->getName($p);
                $cName = $CityModel->getName($c);
                $aName = $AreaModel->getName($a);
                $this->toArray($UserAddress);
                $UserAddress["pca"] = "[$p,$c,$a]";
                $UserAddress["pcaName"] = "['$pName','$cName','$aName']";
            }
            $this->data["addr"] = $UserAddress;
        }else{
            //新增

        }
        return $this->render("App/Wap/address_add.php.twig",$this->data);
    }

    /**
     * @route("/address/add/do",name="UserAppAddressDoAdd")
     */
    public function UserAppAddressDoAdd()
    {
        $uid = $this->needToBeUser();
        if(!$name = $this->getParamRequired("name")){return $this->json_out(0,"参数缺失");}
        if(!$phone = $this->getParamRequired("phone")){return $this->json_out(0,"参数缺失");}
        if(!$address = $this->getParamRequired("address")){return $this->json_out(0,"参数缺失");}
        if(!$province = $this->getParamRequired("province")){return $this->json_out(0,"请选择配送省份");}
        if(!$city = $this->getParamRequired("city")){return $this->json_out(0,"请选择配送城市");}
        if(!$area = $this->getParamRequired("area")){return $this->json_out(0,"请选择配送地区");}
        if(!$isDefault = $this->getParamRequired("isDefault")){$isDefault = 0;}

        if ($isDefault) {
            $time = time();
        }else{
            $time = 0;
        }
        if($id = $this->getParamRequired("id")){
            $Model = new UserAddressModel($this->getDoctrine());
            $Entity = $Model->getById($id);
        }else{
            $Entity = new UserAddress();
        }

        $Entity->setUid($uid)
            ->setName($name)
            ->setPhone($phone)
            ->setAddress($address)
            ->setProvince($province)
            ->setCity($city)
            ->setArea($area)
            ->setAddtime($time);
        $this->myEm()->persist($Entity);
        $this->myEm()->flush();
        return $this->json_out(1,"保存成功","");
    }





//    /**
//     * @route("/address/setDefault",name="UserAppAddressSetDefault")
//     */
//    public function UserAppAddressSetDefault()
//    {
//        if(!$what = $this->getParamRequired("what")){return $this->json_out(0,"参数缺失");}
//
//        return $this->json_out(1,$msg);
//    }



}
